﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.Colors;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
   public class PlottingScale
    {
        /// <summary>
        /// 添加比例尺
        /// </summary>
        /// <param name="db"></param>
        /// <param name="stpointLM"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static void AddPlottingScale(Database db, Point2d stpointLM,ref Extents2d ext, int min = 1640, int max = 1670,int cap=5,double _scale= 1, Unit unit= Unit.Mile)
        {
            List<Point2d> g_pointList = new List<Point2d>();
            Dictionary<Point2d, int> bxDic = new Dictionary<Point2d, int>();
            Point2d point = new Point2d();
            int text = min;
            int textLb = 0;
            int i = 0;
            while (text <= max)
            {
                text = min + cap * i;
                point = stpointLM.C2D(0,cap * i);
                g_pointList.Add(point);
                point = stpointLM.C2D(0, cap * i);
                textLb = text;
                bxDic.Add(point, textLb);
                i++;
            }

            DarwCable(db, ref ext, g_pointList);
            DarwText(db, bxDic,ref ext,_scale);
            //TextPloter.AddDBText(db, g_pointList[g_pointList.Count - 1].C3D(-7, 1),ref ext, "ELEVATION(m)", _scale, 3);//文本添加
            //DrawPolyLines.AddText(g_pointList);
        }
        public static bool DarwText(Database db, Dictionary<Point2d, int> bxDic, ref Extents2d ext, double _scale = 1)
        {
            if (bxDic.Count <= 0)
                return false;

            try
            {
                foreach (Point2d p in bxDic.Keys)
                {
                    TextPloter.AddDBText(db, p.C3D(1* _scale, 0), ref ext, (bxDic[p]/100).ToString(), _scale, 2, "SRB.HT", 0, TextHorizontalMode.TextLeft, TextVerticalMode.TextVerticalMid);//文本添加
                }
            }

            catch (Exception ex)
            {

                return false;
            }

            return true;
        }
        /// <summary>
        /// 绘制线条
        /// </summary>
        /// <returns>bool</returns>
        public static bool DarwCable(Database db, ref Extents2d ext, List<Point2d> g_pointList)
        {
            if (g_pointList.Count <= 0)
                return false;

            try
            {
                for (int i = 1; i < g_pointList.Count; i++)
                {
                    short number = 2;
                    if (i % 2 == 0)
                    {
                        number = 7;
                        addLine(db, g_pointList[i - 1], g_pointList[i], ref ext,false, number);
                    }
                    else
                    {
                        addLine(db, g_pointList[i - 1], g_pointList[i], ref ext, true, number);
                    }

                }
            }

            catch (Exception ex)
            {

                return false;
            }

            return true;
        }

        //添加线
        public static void addLine(Database db, Point2d sp, Point2d ep, ref Extents2d ext, bool isOdd = false, short num = 0)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable; //以写方式打开块表
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],//以写的方式打开模型空间块表记录
                    OpenMode.ForWrite) as BlockTableRecord;
                //Database database = HostApplicationServices.WorkingDatabase;
                //Autodesk.AutoCAD.DatabaseServices.Line line = new Autodesk.AutoCAD.DatabaseServices.Line(g_pointList[i - 1], g_pointList[i]);
                Polyline line = new Polyline() { Closed = true };//定义不封闭的Polyline
                line.AddVertexAt(0, sp, 0, 0, 0);
                line.AddVertexAt(1, ep, 0, 0, 0);
                line.AddVertexAt(2, ep.C2D(0.2*1000, 0), 0, 0, 0);
                line.AddVertexAt(3, sp.C2D(0.2 * 1000, 0), 0, 0, 0);
                Polyline line1 = new Polyline() { Closed = true };//定义不封闭的Polyline
                line1.AddVertexAt(0, sp, 0, 0, 0);
                line1.AddVertexAt(1, ep, 0, 0, 0);
                line1.AddVertexAt(2, ep.C2D(-0.2 * 1000, 0), 0, 0, 0);
                line1.AddVertexAt(3, sp.C2D(-0.2 * 1000, 0), 0, 0, 0);
                Color color = Color.FromColorIndex(ColorMethod.ByColor, num);

                line.Color = color;
                line.LineWeight = LineWeight.LineWeight009;
                //line.Thickness = 2;

                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                line1.Color = color;
                //line1.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight009;
                //line1.Thickness = 2;

                modelSpace.AppendEntity(line1);
                tr.AddNewlyCreatedDBObject(line1, true);
                ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));
                if (isOdd)
                {
                    ObjectIdCollection collection = new ObjectIdCollection();
                    collection.Add(line.ObjectId);

                    Hatch hatch = new Hatch();

                    hatch.Elevation = 0;
                    hatch.HatchStyle = HatchStyle.Normal;
                    hatch.ColorIndex = 7;
                    //hatch.PatternAngle = 0;
                    hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); //设置填充图案
                    hatch.AppendLoop(HatchLoopTypes.Default, collection); //设置填充边界 //
                    hatch.EvaluateHatch(true);
                    modelSpace.AppendEntity(hatch);
                    tr.AddNewlyCreatedDBObject(hatch, true);
                }
                else
                {
                    ObjectIdCollection collection = new ObjectIdCollection();
                    collection.Add(line1.ObjectId);

                    Hatch hatch = new Hatch();

                    hatch.Elevation = 0;
                    hatch.HatchStyle = HatchStyle.Normal;
                    hatch.ColorIndex = 7;
                    //hatch.PatternAngle = 0;
                    hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); //设置填充图案
                    hatch.AppendLoop(HatchLoopTypes.Default, collection); //设置填充边界 //
                    hatch.EvaluateHatch(true);
                    modelSpace.AppendEntity(hatch);
                    tr.AddNewlyCreatedDBObject(hatch, true);
                }
                tr.Commit();
            }
        }

    }
}
